package experiment1;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

/**
 * @author 冯永龙
 * @date 2021/3/9 15:50
 */
public class task2 {

    //将字符排序
    public Set<String> getWordsAlec(String filename) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filename));
        Set<String> wordSet = new TreeSet<>();
        String[] word = bufferedReader.readLine().split("[ \t\\(\\)]");
        int i = 0;

        while ( i< word.length) {
            wordSet.add(word[i]);
            i++;
        }

        return wordSet;
    }

    //记录字符个数
    public Map<String, Integer> getWordsNum(String filename) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filename));
        Map<String, Integer> wordMap = new TreeMap<>();
        String[] word = bufferedReader.readLine().split("[ \t\\(\\)]");
        int count = 1;

        for (String s : word) {
            if (wordMap.get(s) != null) {
                wordMap.put(s, wordMap.get(s) + 1);
            } else {
                wordMap.put(s, count);
            }
        }

        return wordMap;
    }

    //打印字符
    public void printWords(Map<String, Integer> map, Set<String> set) {
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            for (int i = 0; i < map.get(next); i++) {
                System.out.println(next + " ");
            }
        }
    }

}

